package pt.inescid.components.policyengine.heimdall.contact;

import java.util.Vector;

import pt.inescid.components.policyengine.heimdall.base.EventObj;
import pt.inescid.components.policyengine.heimdall.base.User;
import pt.inescid.components.policyengine.heimdall.commons.Log;
import pt.inescid.components.policyengine.heimdall.pap.base.CesDefinition;
import pt.inescid.components.policyengine.heimdall.pap.base.ModelDefinition;
import pt.inescid.components.policyengine.heimdall.pap.base.RuleDefinition;
import pt.inescid.components.policyengine.heimdall.pdp.base.ModelInstance;
import pt.inescid.components.policyengine.heimdall.pip.NodeInfo;

public class Outbound {
	
	/* ===========================================================
	 ==============================================================
	 =============                    =============================
	 ============= query =====================
	 =============                    =============================
	 ==============================================================
	 =========================================================== */
	public static void query(
			NodeInfo currentNode,
			User currentUser, 
			String modelTag, 
			String evaluationInstanceTag, 
			String cesTag,
			EventObj ce,
			String cesName,
			String domainName,
			long lastEventId) {

		Log.debug(Log.INFO, "Arrived. destNode=" +
				currentNode.getHostport(), "Outbound.getAllRecordsAfter");

		ModelDefinition currentModelDef = currentUser.getModelByTag(
				modelTag);
		CesDefinition currentCes = currentModelDef.getCes(cesName);
		RuleDefinition cesGlobalRestriction = 
			currentCes.getGlobalRestriction();
		String cesSqlRestriction = 
			cesGlobalRestriction.getSqlRestriction(currentUser, ce);

		// parameters to send:
		Vector allParameters = new Vector();

		allParameters.add(new Long(currentUser.getId()));
		allParameters.add(modelTag);
		allParameters.add(evaluationInstanceTag);
		allParameters.add(cesName);
		allParameters.add(cesTag);
		allParameters.add(cesSqlRestriction);
		
//		allParameters.add(ce);
//		allParameters.add(domainName);
		
		
		RemoteCommand command = new RemoteCommand(
				RemoteCommand.QUERY_CES_COMMAND,
				allParameters);
		command.setHostname(currentNode.getHostname());
		command.setHostport(currentNode.getHostport());
		
		OutboundThread worker = new OutboundThread(command);
		worker.start();
 	}; // query
	

	
 	
 	
	
	
	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      exportCes       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
	public static void exportCes(
			String hostname,
			int hostport,
			User currentUser, 
			String localModelTag,
			String cesName) {

		Log.debug(Log.INFO, "Arrived. destNode=" +
				hostport, "Outbound.getAllRecordsAfter");

		// parameters to send:
		// - user id
		// - policy home hostname/port
		// - policy text
		// - cesName
		// - cesId
		Vector allParameters = new Vector();
		allParameters.add(new Long(currentUser.getId()));
		allParameters.add(hostname);
		allParameters.add(new Integer(hostport));
		allParameters.add(currentUser.getModelByTag(localModelTag).
				getModelAsText());
		allParameters.add(cesName);
		
        RemoteCommand command = new RemoteCommand(
        		RemoteCommand.LOAD_CES_COMMAND,
        		allParameters);
		command.setHostname(hostname);
		command.setHostport(hostport);
		
        
		new OutboundThread(command).start();
	} // exportCes
	

	
	
	
	
	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      requestRemoteEvaluation       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
	public static void requestRemoteEvaluation(
			ModelInstance currentModel, 
			String policyName, 
			NodeInfo currentNode) {

		Log.debug(Log.INFO, "Arrived. destNode=" +
				currentNode.getHostport(), "Outbound.requestRemoteEvaluation");

		// parameters to send:
		// - user id
		// - model def tag
		// - model instance tag
		// - ces name
		// - cesSqlRestriction
		Vector allParameters = new Vector();
		allParameters.add(new Long(currentModel.getUserInfo().getId()));
		allParameters.add(currentModel.getCe());
		allParameters.add(currentModel.getDefinition().getTag());
		allParameters.add(currentModel.getTag());
		allParameters.add(policyName);
		
		RemoteCommand command = new RemoteCommand(
				RemoteCommand.REMOTE_EVALUATION_COMMAND,
				allParameters);
		command.setHostname(currentNode.getHostname());
		command.setHostport(currentNode.getHostport());
		
		OutboundThread worker = new OutboundThread(command);
		worker.start();
	} // requestRemoteEvaluation
	
	
	
	
	
	
	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      testSystem       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
	public static void testComm(NodeInfo currentNode) {

        RemoteCommand command = new RemoteCommand(
        		RemoteCommand.TEST_COMMAND,
        		null);
		command.setHostname(currentNode.getHostname());
		command.setHostport(currentNode.getHostport());
        
        OutboundThread worker = new OutboundThread(command);
		worker.start();
	} // testComm
	

	
	
	
	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      sendResponse       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
	public static void sendResponse(
			RemoteResponse response) {

		Vector allParameters = new Vector();
		allParameters.add(response);

		RemoteCommand command = new RemoteCommand(
        		RemoteCommand.RESPONSE_COMMAND,
        		allParameters);
		command.setHostname(response.getHostname());
		command.setHostport(response.getHostport());
       
		new OutboundThread(command).start();
	} // testComm
	

	
}; // RemoteInterface
